using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Dogfight2008.Common;
using System.IO;
using OpenTK.OpenGL;
using OpenTK.Math;
using OpenTK.OpenGL.Enums;

namespace SpinningB
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();

      Application.Idle += new EventHandler(Application_Idle);
    }

    void Application_Idle(object sender, EventArgs e)
    {
      rot += 0.3f;
      glControl1.Invalidate();
    }

    ColoredPolygonMesh mesh = null;
    float rot = 0;

    private void Form1_Load(object sender, EventArgs e)
    {
      GLControlUtil.Init(glControl1, 1000);
      GLUtil.SunLightDirection(0, 1, 0);
      mesh = CplLoader.FromString(File.ReadAllText("B.cpl"));
    }

    private void glControl1_Resize(object sender, EventArgs e)
    {
      GLControlUtil.HandleResize(glControl1, 1000);
    }

    private void glControl1_Paint(object sender, PaintEventArgs e)
    {
      GLUtil.Clear();

      GL.Translate(-1, 10, -1);

      GLUtil.DrawXYZAxis(5);

      //DrawTriangle();

      GL.Rotate(rot, Vector3.UnitZ);
      GL.Translate(0, 0, 0.1);
      GLUtil.DrawMesh(mesh);

      GLControlUtil.SwapBuffers(glControl1);
    }

    private static void DrawTriangle()
    {
      GL.Begin(BeginMode.Triangles);
      GL.Color3(1.0, 1.0, 1.0);
      GL.Normal3(0, 1, 0);
      GL.Vertex3(0, 1, 0);
      GL.Vertex3(1, 1, 0);
      GL.Vertex3(0, 1, 1);
      GL.End();
    }
  }
}